package de.ff7rl.core;

import java.util.HashMap;
import java.util.Map;

import de.ff7rl.core.gamestate.GameState;
import de.ff7rl.core.gamestate.GameStateController;
import de.ff7rl.core.map.DungeonMap;
import de.ff7rl.core.party.Party;
import de.ff7rl.core.reference.MapReference;
import de.ff7rl.core.serialize.SaveState;
import de.ff7rl.ui.Terminal;

public class Game {

  private final Map<GameState, GameStateController> gameStateControllers = new HashMap<GameState, GameStateController>();
  private final DungeonMap map = MapReference.getMap("editor2");
  private GameState gameState;
  private Terminal terminal;
  private final Party party = Party.INITIAL_PARTY;

  public Game() {
    for (final GameState gameStateValue : GameState.values()) {
      final GameStateController gameStateController = gameStateValue.getController();
      gameStateController.initialize(this);
      gameStateControllers.put(gameStateValue, gameStateController);
    }
  }

  public GameStateController getGameStateController() {
    return gameStateControllers.get(gameState);
  }

  public DungeonMap getMap() {
    return map;
  }

  public Party getParty() {
    return party;
  }

  public Terminal getTerminal() {
    return terminal;
  }

  public void initialize(final Terminal theTerminal) {
    terminal = theTerminal;
  }

  public void load(final SaveState theSaveState) {
    // TODO implement me
  }

  public void newGameState(final GameState theNewGameState, final Object... theArguments) {
    final GameStateController gameStateControllerToLeave = getGameStateController();
    if (gameStateControllerToLeave != null) {
      gameStateControllerToLeave.leave(theNewGameState);
    }
    final GameState oldGameState = gameState;
    gameState = theNewGameState;
    final GameStateController gameStateControllerToEnter = getGameStateController();
    if (gameStateControllerToEnter != null) {
      gameStateControllerToEnter.enter(oldGameState, theArguments);
    }
  }

  public void save(final SaveState theSaveState) {
    // TODO implement me
  }

}
